package uk.ac.aber.Blockmation.Tests;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;

/**
 *  1. loads colours form rbgcolours.txt
 *  2. loads character assosiations from colourcharacters.txt
 *  3. Generates code to place inside OurColors class
 * 
 * @author alex
 */
public class CreateColourCode {
    
    // Main method
    public static void main (String args[]) {
        // Stream to read file
        DataInputStream dis = null;
        Scanner scanner = new Scanner(System.in);
        String input = null;
        String line = null;

        ArrayList<String> fileArray = new ArrayList<String>();
        
        // Read rgb file into Array and print output
        try {
            
            //System.out.println("Enter filename:");
            //input = scanner.next();
            input = "src/uk/ac/aber/Blockmation/Tests/rgbcolours.txt";
            
            // Open an input stream
            File f = new File(input); 
            FileInputStream fis = new FileInputStream(f); 
            BufferedInputStream bis = new BufferedInputStream(fis); 
            dis = new DataInputStream(bis);

            // Read a txt line by line
            while ( (line=(String) dis.readLine() ) != null ) {
                fileArray.add( line );
                //System.out.println( line );
            }

            // Close our input stream
            fis.close();
            
        } catch (IOException e) {
                System.err.println ("Unable to read from file");
                System.exit(-1);
        }
        
        //
        //for (int i = 0; i < 500; i++) {
            //char c = (char)i;
            //System.out.println(""+c);
            
        //}
        
        // Read colourcharacter file into Array and print ouput
        // note - colourcharacter.txt ommits b,r,l,a as they are previously assigned
        ArrayList<String> charArray = new ArrayList<String>();
        try {
            
            //System.out.println("Enter filename:");
            //input = scanner.next();
            //input = "src/uk/ac/aber/Blockmation/Tests/colourcharacters.txt";
            input = "src/uk/ac/aber/Blockmation/Tests/unicode.txt";
            // Open an input stream
            File f = new File(input);
            FileReader freader = new FileReader(f);
            BufferedReader buffreader = new BufferedReader(freader);
            
            /*FileInputStream fis = new FileInputStream(f); 
            BufferedInputStream bis = new BufferedInputStream(fis); 
            dis = new DataInputStream(bis);*/

            // Read a txt line by line
            while ( (line=(String) buffreader.readLine() ) != null ) {
                line = line.substring(5,6);
                charArray.add( line );
                System.out.println( line );
            }

            // Close our input stream
            buffreader.close();
            
        } catch (IOException e) {
                System.err.println ("Unable to read from file");
                System.exit(-1);
        }
        
        try {
            
            //System.out.println("Enter filename:");
            //input = scanner.next();
            //input = "src/uk/ac/aber/Blockmation/Tests/colourcharacters.txt";
            input = "src/uk/ac/aber/Blockmation/Tests/unicode2.txt";
            // Open an input stream
            File f = new File(input);
            FileReader freader = new FileReader(f);
            BufferedReader buffreader = new BufferedReader(freader);
            
            /*FileInputStream fis = new FileInputStream(f); 
            BufferedInputStream bis = new BufferedInputStream(fis); 
            dis = new DataInputStream(bis);*/

            // Read a txt line by line
            while ( (line=(String) buffreader.readLine() ) != null ) {
                line = line.substring(6,7);
                charArray.add( line );
                System.out.println( line );
            }

            // Close our input stream
            buffreader.close();
            
        } catch (IOException e) {
                System.err.println ("Unable to read from file");
                System.exit(-1);
        }
        
        // Create Ouput
        
        String output = "";
        for (int i=0; i < fileArray.size()-1; i++) {
            
           // while (i < charArray.size()) {
            
                String red = fileArray.get(i).substring(0, 1);
                int redNum = convertToDecimal(red.charAt(0));
                
                String green = fileArray.get(i).substring(1, 2);
                int greenNum = convertToDecimal(green.charAt(0));
                
                String blue = fileArray.get(i).substring(2, 3);
                int blueNum = convertToDecimal(blue.charAt(0));
                
                
                

                output += "this.add('"+ charArray.get(i) +
                        "', new Color(" + redNum +", " + greenNum +", " + blueNum +"));";
                output += "\n";
            
            //}
        }
        
        System.out.println(output);
        
        System.out.println(charArray.size());
        
    }
    
    public static int convertToDecimal(char input) {
        int output = 0;
        
        switch (input) {
            case '0':
                output = 0;
                break;
            case '3':
                output = 51;
                break;
            case '6':
                output = 102;
                break;
            case '9':
                output = 153;
                break;
            case 'C':
                output = 204;
                break;
            case 'F':
                output = 255;
                break;
        }
        
        return output;
    }

}
